// 封装请求对象
const BASE_URL = 'https://api.imooc-blog.lgdsunday.club/api';
/**
 * 请求工具
 * @param {url, data, method} param0 
 * @returns 
 */
function request({ url, data, method }) {
    return new Promise((resolve, reject) => {
        // uni-zpp.request 发起网络请求
        uni.request({
            url: BASE_URL + url,
            data: data,
            method: method,
            success: ({ data, statusCode, header }) => {
                if (data.success) {
                    // 请求成功
                    resolve(data);
                } else {
                    // 请求失败，给用户提示
                    uni.showToast({
                        title: data.message,
                        icon: 'none',
                        mask: true,
                        duration: 3000
                    });
                    reject(data.message);
                }
            },
            fail: (error) => {
                reject(error);
            }
        });
    });
}

export default request;